from PySide6.QtCore import Qt, Signal
from qfluentwidgets import SettingCard, FluentIconBase
from qfluentwidgetspro import DropDownColorPalette
from app.common.config import cfg
from PySide6.QtWidgets import QHBoxLayout, QWidget
class ColorPaletteSettingCard(SettingCard):
    """ 双颜色选择器设置卡片 """
    
    colorChanged = Signal(str, str)  # 发送两个颜色值的信号

    def __init__(self, icon: FluentIconBase, title: str, content: str, 
                 configItem1, configItem2, parent=None):
        super().__init__(icon, title, content, parent)
        self.configItem1 = configItem1  # 第一个颜色的配置项
        self.configItem2 = configItem2  # 第二个颜色的配置项
        
        # 创建两个颜色选择器
        self.colorPalette1 = DropDownColorPalette(self)
        self.colorPalette2 = DropDownColorPalette(self)
        
        # 设置初始颜色值
        self.colorPalette1.setDefaultColor(self.configItem1.value)
        self.colorPalette2.setDefaultColor(self.configItem2.value)
        self.colorPalette1.setColor(self.configItem1.value)
        self.colorPalette2.setColor(self.configItem2.value)
        
        # 设置最小宽度
        self.colorPalette1.setMinimumWidth(100)
        self.colorPalette2.setMinimumWidth(100)
        
        # 创建水平布局来容纳两个颜色选择器
        
        colorContainer = QWidget()
        colorLayout = QHBoxLayout(colorContainer)
        colorLayout.setContentsMargins(0, 0, 0, 0)
        colorLayout.addWidget(self.colorPalette1)
        colorLayout.addWidget(self.colorPalette2)
        colorLayout.setSpacing(8)  # 设置两个选择器之间的间距
        
        # 将颜色选择器容器添加到右侧
        self.hBoxLayout.addWidget(colorContainer, 0, Qt.AlignRight)
        
        # 连接信号
        self.colorPalette1.colorChanged.connect(lambda c: self._onColorChanged(1, c))
        self.colorPalette2.colorChanged.connect(lambda c: self._onColorChanged(2, c))

    def _onColorChanged(self, palette_index: int, color: str):
        """ 颜色改变时的处理 """
        if palette_index == 1:
            self.configItem1.value = color
        else:
            self.configItem2.value = color
            
        cfg.save()
        self.colorChanged.emit(self.configItem1.value, self.configItem2.value)

    def getColors(self):
        """ 获取当前的两个颜色值 """
        return self.colorPalette1.currentColor(), self.colorPalette2.currentColor()

    def setColors(self, color1: str, color2: str):
        """ 设置两个颜色值 """
        self.colorPalette1.setCurrentColor(color1)
        self.colorPalette2.setCurrentColor(color2)